// pages/resume-pages/job-experience/job-experience.js
const _month = [1,2,3,4,5,6,7,8,9,10,11,12];
import Toast from '../../../component/toast/toast';
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pickerDate:[[] , _month],
        startDateIndexs:[0,0],
        endDateIndexs:[0,0],
        startWorkDate:'',
        endWorkDate:'',
        componyName:'',
        positionName:'',
        workContent:'',
        flag:0,
        resumeId:''
    },
    get20Year(){
        let currentYear = parseInt(new Date().getFullYear());
        let year = currentYear;
        let yearsArr = [];
        for (let i = 1980; i <= currentYear; i++) {
            yearsArr.push(year--);
        }
        return yearsArr;
    },
    // 开始工作日期picker
    startDatePickerChange(e){
        let pickerDate = this.data.pickerDate;
        let startDateIndexs = this.data.startDateIndexs;
        let startWorkDate = pickerDate[0][startDateIndexs[0]] + "-" + pickerDate[1][startDateIndexs[1]];
        this.setData({
            startWorkDate: startWorkDate
        });
    },
    startDateItemPickerChange(e){
        let data = {
            startDateIndexs: this.data.startDateIndexs
        }
        if (e.detail.column == 0) {
            data.startDateIndexs[0] = e.detail.value;
        } else if (e.detail.column == 1) {
            data.startDateIndexs[1] = e.detail.value;
        }
        this.setData(data);
    },
    //结束工作日期picker
    endDatePickerChange(e){
        let pickerDate = this.data.pickerDate;
        let endDateIndexs = this.data.endDateIndexs;
        let endWorkDate = pickerDate[0][endDateIndexs[0]] + "-" + pickerDate[1][endDateIndexs[1]];
        this.setData({
            endWorkDate: endWorkDate
        });
    },
    endDateItemPickerChange(e){
        let data = {
            endDateIndexs: this.data.endDateIndexs
        }
        if (e.detail.column == 0) {
            data.endDateIndexs[0] = e.detail.value;
        } else if (e.detail.column == 1) {
            data.endDateIndexs[1] = e.detail.value;
        }
        this.setData(data);
    },
    //输入公司名称
    inputConpony(e){
        this.setData({
            componyName:e.detail.value
        });
    },
    //输入职位信息
    inputPosition(e) {
        this.setData({
            positionName: e.detail.value
        });
    },
    //输入工作内容
    inputWorkContent(e){
        this.setData({
            workContent: e.detail.value
        });
    },
    //保存
    clickSave(){
        let componyName = this.data.componyName;
        if (!componyName){
            Toast("请填写公司名称");
            return;
        }
        let positionName = this.data.positionName;
        if(!positionName){
            Toast("请填写所在职位");
            return;
        }
        let startWorkDate = this.data.startWorkDate;
        if(!startWorkDate){
            Toast("请填写开始工作时间");
            return;
        }
        let endWorkDate = this.data.endWorkDate;
        if(!endWorkDate){
            Toast("请填写结束时间");
            return;
        }
        let workContent = this.data.workContent;
        if(!workContent){
            Toast("请输入工作内容");
            return;
        }
        let data = {
            EnterpriseName: componyName,
            PositionName: positionName,
            StartDate: startWorkDate,
            EndDate: endWorkDate,
            WorkContent: workContent
        }
        if(this.data.flag == 0){
            data.Id = app.userInfo.Id;
            data.ResumeInfoId = app.globalData.resumeInfo.Id;
            this.addResumeWorkExperience(data);
        }
        else if(this.data.flag == 1){
            data.Id = this.data.resumeId;
            this.editResumeWorkExperience(data);
        }
        console.log(data);
    },
    //删除工作简历
    deleteExperience(){
        let that = this;
        wx.showModal({
            title: '温馨提示',
            content: '您确定要删除当前经历吗？',
            showCancel: true,
            cancelText: '取消',
            cancelColor: '#4EB262',
            confirmText: '确定',
            confirmColor: '#222',
            success: function (res) {
                if (res.confirm) {
                    that.deleteResumeWork();
                }
            }
        })
    },
    deleteResumeWork(){
        let data = {
            CurrentUserInfoId: app.userInfo.Id,
            ResumeInfoWorkExperienceId: this.data.resumeId
        }
        wx.showLoading({
            title: '提交中...',
            mask: true
        });
        wx.request({
            url: app.globalData.serverAddress.deleteResumeWorkExperience,
            data: data,
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            method: 'POST',
            success: function (res) {
                console.log("deleteResumeWorkExperience response", res);
                if (res.statusCode == 200) {
                    if (res.data.CodeStatus == 20000) {
                        wx.navigateBack({
                            delta: 1
                        })
                    } else {
                        Toast(res.data.Message);
                    }
                } else {
                    Toast("删除失败，请检查网络！");
                }
            },
            fail: function (res) { console.log("deleteResumeWorkExperience fail", res) },
            complete: function (res) { wx.hideLoading() },
        })
    },

    //新增工作简历
    addResumeWorkExperience(data){
        wx.showLoading({
            title: '提交中...',
            mask: true
        });
        wx.request({
            url: app.globalData.serverAddress.addResumeWorkExperience,
            data: data,
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            method: 'POST',
            success: function(res) {
                console.log("addResumeWorkExperience response" , res);
                if(res.statusCode == 200){
                    if (res.data.CodeStatus == 20000){
                        wx.navigateBack({
                            delta: 1
                        })
                    }else{
                        Toast(res.data.Message);
                    }
                }else{
                    Toast("保存失败，请检查网络！");
                }
            },
            fail: function (res) { console.log("addResumeWorkExperience fail" , res)},
            complete: function(res) {wx.hideLoading()},
        })
    },
    //编辑工作经历
    editResumeWorkExperience(data){
        wx.showLoading({
            title: '提交中...',
            mask: true
        });
        wx.request({
            url: app.globalData.serverAddress.editResumeWorkExperience,
            data: data,
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            method: 'POST',
            success: function (res) {
                console.log("editResumeWorkExperience response", res);
                if (res.statusCode == 200) {
                    if (res.data.CodeStatus == 20000) {
                        wx.navigateBack({
                            delta: 1
                        })
                    } else {
                        Toast(res.data.Message);
                    }
                } else {
                    Toast("保存失败，请检查网络！");
                }
            },
            fail: function (res) { console.log("editResumeWorkExperience fail")},
            complete: function (res) {wx.hideLoading();},
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let years = this.get20Year();
        let key = `pickerDate[0]`
        this.setData({
            [key]:years
        });
        if(options.parmas){
            let obj = JSON.parse(options.parmas);
            this.setData({
                startWorkDate: obj.StartDate.substring(0, obj.StartDate.lastIndexOf('-')),
                endWorkDate: obj.EndDate.substring(0, obj.EndDate.lastIndexOf('-')),
                componyName: obj.EnterpriseName,
                positionName: obj.PositionName,
                workContent: obj.WorkContent,
                resumeId:obj.Id,
                flag:1
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})